class Solution {
    public static class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
    }
    long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;

        boolean left = isValidBST(root.left);
        if (left == false) return false;

        boolean cur = false;
        if (root.val > prev) cur = true;
        if (cur == false) return false;
        prev = root.val;

        boolean right = isValidBST(root.right);
        return left && cur && right;
    }
}